package com.dys.controller;

import com.dys.service.CpService;
import com.dys.service.CplbService;
import com.dys.tools.UploadUtils;
import com.dys.util.Cp;
import com.dys.util.Cplb;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
@Controller
@RequestMapping("/cp")
public class CpController {
    @Autowired
    private CpService cpService;
    @Autowired
    private CplbService cplbService;



    @RequestMapping("/getAllCp")
    public String getAllCp(Model model){
        List<Cp> list = cpService.getAllCp();
        model.addAttribute("list",list);
        return "cpgl";
    }

    //已删除数据
    @RequestMapping("/getDelCp")
    public String getDelCp(Model model) {
        List<Cp> dellist = cpService.getDelCp();
        model.addAttribute("dellist",dellist);
        return "delcpgl";
    }

    @RequestMapping("/relDelCp")
        public String relDelcp(int id){
            cpService.cpRelDel(id);
            return "redirect:/cp/getDelCp.do";
        }
    /**
     * 还原菜品
     */

    @RequestMapping("/reBackCp")
        public String reBackCp(int id){
        cpService.reBackCp(id);
        return "redirect:/cp/getAllCp.do";
    }


    @RequestMapping("/toTj")
    public String toTj(Model model) {
        List<Cplb> lbList = cplbService.getAll();
        model.addAttribute("lblist",lbList);
        return "cptj";
    }

    @RequestMapping("/cpAdd")
    public String cpAdd(Cp cp, MultipartFile file, HttpServletRequest request) throws IOException {
        String uplodAddress = new FileUploadUtils().upload(file, request);
        cp.setCpPicInsert(uplodAddress);
        cpService.cpAdd(cp);

        return "redirect:/cp/getAllCp.do";
    }

    @RequestMapping("/cpUpdate")
    public String cpUpdate(@Param("id") int id, Model model) {
        Cp cp = cpService.getCpById(id);
        model.addAttribute(cp);
        return "cpxiug";
    }

    @RequestMapping("/cpUpdate2")
    public String cpUpdate2(Cp cp) {
        cpService.cpUpdate2(cp);
        return "redirect:/cp/getAllCp.do";
    }

    @RequestMapping("/cpDel")
    public String cpDel(int id) {
        cpService.cpDelete(id);
        return "redirect:/cp/getAllCp.do";
    }

//    添加图片
//    @RequestMapping("/picInsert")
//    public int cpPicInsert(String picAddress) {
//      return cpService.cpPicInsert(picAddress);
//    }
}
